package com.ysw.wisdomcampus

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import android.widget.ImageView
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import com.google.android.material.textfield.TextInputLayout
import com.ysw.wisdomcampus.kt.glide
import com.ysw.wisdomcampus.kt.start
import com.ysw.wisdomcampus.model.ProjectModel.Companion.PASSWORD
import com.ysw.wisdomcampus.model.ProjectModel.Companion.STATUDENT_ID
import com.ysw.wisdomcampus.model.projectModel
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_login.view.*
import kotlinx.coroutines.*

/**
 * 协程 Coroutines
 */
class LoginActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        // 设置为全屏模式
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        projectModel.initDb()
        num.editText?.apply {
            addTextChangedListener {
                projectModel.query(
                    condition1 = STATUDENT_ID,
                    conditionValue1 = this.text.toString()
                )?.let {
                    setIcon(path1 = it.icon)
                }
            }
        }

        login.setOnClickListener {
            GlobalScope.launch(Dispatchers.Main) {
                it.isVisible = false
                progress.isVisible = true
                val num = num.editText?.text.toString().trim()
                val password = ps.editText?.text.toString().trim()
                val succeed: Boolean = withContext(Dispatchers.IO) {
                    delay(2000)
                    projectModel.query(
                        condition1 = STATUDENT_ID,
                        conditionValue1 = num,
                        condition2 = PASSWORD,
                        conditionValue2 = password
                    )?.let {
                        projectModel.activeStudent = it
                        return@withContext true
                    } ?: false
                }
                if (succeed) {
                    finish()
                    start(this@LoginActivity) {
                        Intent(
                            this@LoginActivity,
                            MainActivity::class.java
                        ).apply {
                            putExtra(STATUDENT_ID, num)
                        }
                    }
                } else {
                    it.isVisible = true
                    progress.isVisible = false
                    projectModel.toast("用户名或密码不正确")
                }
            }
        }
    }

    fun setIcon(
        path1: String = "",
        resId1: Int = 0
    ) {
        glide(this, resId = resId1, path = path1, view = appIcon)
    }
}